package com.threeox.commonlibrary.ui.view.tableview;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.ui.view.inter.table.ITablePagingView;
import com.threeox.commonlibrary.ui.view.inter.table.OnTablePagingListener;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.ToastUtils;
import com.threeox.utillibrary.util.res.LayoutUtils;

/**
 * 
 * @ClassName: CommonTablePagingView
 * 
 * @Description: TODO(表格分页组件)
 * 
 * @author 赵屈犇
 * 
 * @date 创建时间: 2017/8/15 14:04
 * 
 * @version 1.0
 * 
 */
public class CommonTablePagingView extends LinearLayout implements View.OnClickListener, ITablePagingView {

    private Context mContext = null;

    private EditText mPageNumberView;

    private Long mMaxPageNumber = 1L;
    private Integer mNowPageNumber = 1;
    private OnTablePagingListener mOnTablePagingListener;

    public CommonTablePagingView(Context context) {
        super(context);
        this.mContext = context;
        setView();
        initView();
        setListener();
    }

    private void setView() {
        LayoutUtils.inflate(mContext, R.layout.view_table_paging, this);
    }

    private void initView() {
        mPageNumberView = (EditText) findViewById(R.id.page_number_view);
    }

    private void setListener() {
        findViewById(R.id.home_page_view).setOnClickListener(this);
        findViewById(R.id.previous_page_view).setOnClickListener(this);
        findViewById(R.id.next_page_view).setOnClickListener(this);
        findViewById(R.id.final_page_view).setOnClickListener(this);
        findViewById(R.id.jump_page_view).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mOnTablePagingListener != null) {
            int id = v.getId();
            if (R.id.home_page_view == id) {
                mOnTablePagingListener.homePage();
            } else if (R.id.previous_page_view == id) {
                mOnTablePagingListener.previousPage();
            } else if (R.id.next_page_view == id) {
                mOnTablePagingListener.nextPage();
            } else if (R.id.final_page_view == id) {
                mOnTablePagingListener.finalPage();
            } else if (R.id.jump_page_view == id) {
                if (mPageNumberView != null) {
                    String result = mPageNumberView.getText().toString();
                    if (EmptyUtils.isNotEmpty(result)) {
                        try {
                            int pageNumber = Integer.parseInt(result);
                            if (pageNumber > mMaxPageNumber) {
                                ToastUtils.showShortToast(mContext, "此表只有" + mMaxPageNumber + "页!");
                                mPageNumberView.setText("");
                                return;
                            }
                            mOnTablePagingListener.jumpPage(pageNumber);
                        } catch (NumberFormatException e) {
                            ToastUtils.showShortToast(mContext, "页码只能为整数!");
                        }
                    } else {
                        ToastUtils.showShortToast(mContext, "页码不能为空!");
                    }
                }
            }
        }
    }

    @Override
    public void setOnTablePagingListener(OnTablePagingListener onTablePagingListener) {
        this.mOnTablePagingListener = onTablePagingListener;
    }

    @Override
    public void setNowPageNumber(Integer nowPageNumber) {
        this.mNowPageNumber = nowPageNumber;
        if (nowPageNumber != null && getMaxPageNumber() != null)
            mPageNumberView.setHint(nowPageNumber +  "/" + getMaxPageNumber());
    }

    @Override
    public void setMaxPageNumber(Long maxPageNumber) {
        this.mMaxPageNumber = maxPageNumber;
        if (maxPageNumber != null)
            mPageNumberView.setHint(mNowPageNumber + "/" + maxPageNumber);
    }

    @Override
    public Long getMaxPageNumber() {
        return mMaxPageNumber;
    }
}
